package de.gwtprime.client.widgets;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.ArticleSectionPostedEvent;
import de.gwtprime.client.event.CommentPostedEvent;
import de.gwtprime.client.event.handler.ArticleSectionPostedEventHandler;
import de.gwtprime.client.event.handler.CommentPostedEventHandler;
import de.gwtprime.client.places.ArticlePlace;
import de.gwtprime.client.views.ArticleView.Presenter;
import de.gwtprime.shared.requestFactory.ArticleCommentProxy;
import de.gwtprime.shared.requestFactory.ArticleProxy;
import de.gwtprime.shared.requestFactory.ArticleSectionProxy;
import de.gwtprime.shared.util.AppUtilsShared;

public class ArticleWidget extends Composite {
	
	private Presenter presenter;
	
	ArticleWidgetUiBinder uiBinder = GWT.create(ArticleWidgetUiBinder.class);

	@UiField Label articleTitle;
	@UiField Label articleDescription;
	
	@UiField VerticalPanel sections;
	
	@UiField SimplePanel addSectionPanel;
	
	@UiField VerticalPanel commentsPanel;
	@UiField SimplePanel addCommentPanel;
	@UiField Image image;
	
	@UiField Button goNextArticle;
	@UiField Button goPrevArticle;

	interface ArticleWidgetUiBinder extends UiBinder<Widget, ArticleWidget> {
	}

	public ArticleWidget(final ArticleProxy article,
			final ClientFactory clientFactory) {
		initWidget(uiBinder.createAndBindUi(this));

		articleTitle.setText(article.getTitle());
		articleDescription.setText(article.getText());
		
		if(AppUtilsShared.isNullOrEmpty(article.getImage())){
			image.setUrl("img/preview_placeholder.png");
		}else{
			image.setUrl("img/"+article.getImage());
			
		}
		
		image.setAltText("preview image");
		
		for (ArticleSectionProxy section : article.getSections()) {
			sections.add(new ArticleSectionWidget(section, article, clientFactory));
		}
		
		if(clientFactory.getUser()!=null && clientFactory.getUser().isAdmin()){
			addSectionPanel.add(new ArticleSectionEditWidget(article, clientFactory));
		}
		
		addCommentPanel.add(new AddCommentWidget(clientFactory, article));
		
		
		for(ArticleCommentProxy comment : article.getComments()){
			commentsPanel.add(new CommentWidget(comment, clientFactory));
		}
		
		addHandlerForCommentsPosted(clientFactory);
		addHandlerForArticleSectionsPosted(article, clientFactory);
		
		

		goNextArticle.setVisible(article.getNextArticle()!=null);
		
		goNextArticle.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(article.getNextArticle()!=null){
					presenter.goTo(new ArticlePlace(article.getNextArticle().getId().toString()));
				}
			}
		});
		
		goPrevArticle.setVisible(article.getPrevArticle()!=null);
		
		goPrevArticle.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(article.getPrevArticle()!=null){
					presenter.goTo(new ArticlePlace(article.getPrevArticle().getId().toString()));
				}
			}
		});
		

	}

	private void addHandlerForArticleSectionsPosted(final ArticleProxy article,
			final ClientFactory clientFactory) {
		clientFactory.getEventBus().addHandler(ArticleSectionPostedEvent.TYPE, new ArticleSectionPostedEventHandler() {
			
			@Override
			public void onArticleSectionPosted(ArticleSectionPostedEvent event) {
				sections.add(new ArticleSectionWidget(event.getArticleSection(), article, clientFactory));
				
			}
		});
	}

	private void addHandlerForCommentsPosted(final ClientFactory clientFactory) {
		clientFactory.getEventBus().addHandler(CommentPostedEvent.TYPE, new CommentPostedEventHandler() {
			
			@Override
			public void onCommentPosted(CommentPostedEvent event) {
				
				commentsPanel.add(new CommentWidget(event.getArticleComment(), clientFactory));
				
			}
		});
	}

	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
}
